﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PDX
{
    /// <summary>
    /// A simple model that represents an <see cref="IPropertyModel"/>'s validation
    /// state. Small and bindable. The real work is deferred to the
    /// <see cref="IValidationAdapter"/>
    /// </summary>
    public class ValidationModel : INPC
    {
        IValidationAdapter adapter;
        IPropertyModel propModel;

        /// <summary>
        /// Initializes a new instance of the <see cref="ValidationModel"/> class.
        /// </summary>
        /// <param name="adapter">The adapter.</param>
        /// <param name="propModel">The prop model.</param>
        public ValidationModel(IValidationAdapter adapter, IPropertyModel propModel)
        {
            this.adapter = adapter;
            this.propModel = propModel;
            adapter.ValidationChanged += adapter_ValidationChanged;

            Update();
        }

        void adapter_ValidationChanged(object sender, EventArgs e)
        {
            Update();
        }

        /// <summary>
        /// Updates this instance.
        /// </summary>
        void Update()
        {
            var message = adapter.GetMessage(propModel);
            this.Message = message;
        }

        string message;
        /// <summary>
        /// Gets or sets the validation message.
        /// </summary>
        /// <value>
        /// The message.
        /// </value>
        public string Message
        {
            get { return message; }
            set
            {
                if (message != value)
                {
                    message = value;
                    OnPropertyChanged("Message");
                    OnPropertyChanged("HasMessage");
                }
            }
        }

        /// <summary>
        /// Gets a value indicating whether this instance has a <see cref="Message"/>.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance has a message; otherwise, <c>false</c>.
        /// </value>
        public bool HasMessage
        {
            get
            {
                return !string.IsNullOrEmpty(Message);
            }
        }
    }
}
